#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

#define ONE (1<<0) //1
#define TWO (1<<1) //2
#define THREE (1<<2) //4
#define FOUR (1<<3) //8
void show(int flag)
{
    if(flag&ONE) printf("this is function 1\n");
    if(flag&TWO) printf("this is function 2\n");
    if(flag&THREE) printf("this is function 3\n");
    if(flag&FOUR) printf("this is function 4\n");
}

//类似flag的原理
void test1()
{
    printf("--------------------------\n");
    show(ONE);
    printf("--------------------------\n");
    show(ONE|THREE);
    printf("--------------------------\n");
    show(ONE|FOUR);
    printf("--------------------------\n");
    show(ONE|TWO|FOUR);
    printf("--------------------------\n");
}

void test2()
{
    umask(0);
    //最终权限=起始权限&(~umask) 系统默认umask为0002
    //           文件       选项           权限
    //int fd=open("log.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);//文件描述符
    int fd=open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0666);//文件描述符

    if(fd<0)
    {
        printf("open file error\n");
        return;
    }
    const char* message="linux is a great tool\n";
    write(fd,message,strlen(message));
    close(fd);
}

void test3()
{
    char buff[1024];
    int s=read(0,buff,sizeof(buff));
    if(s<0) exit(1);
    buff[s]='\0';
    printf("%s",buff);

}

int main()
{
    printf("stdin->%d\n",stdin->_fileno);
    printf("stdout->%d\n",stdout->_fileno);
    printf("stderr->%d\n",stderr->_fileno);

    //test3();
    //test2();
    //test1();
    ////chdir("/home/shorekeeper");
    //printf("%d\n",getpid());
    ////打开文件的路径和文件名，默认在当前路径下创建一个文件
    ////FILE*fd=fopen("data.txt","w");
    //FILE*fd=fopen("data.txt","a");
    //if(fd==NULL)
    //{
    //    perror("fopen");
    //    return 1;
    //}
    //const char*message="linux is a great tool";
    //
    ////fwrite(message,strlen(message)+1,1,fd);
    //fprintf(fd,"%s:%d\n",message,1234);
    //fclose(fd);
    ////sleep(1000);
    return 0;
}
